#!/usr/bin/ruby
require 'rubygems'
require 'mechanize'
require 'date'
require 'net/smtp'
require 'base64'
require 'scanf'

$all_price={};
def get_data( file_path )
	File.open(file_path,"r").each_line do |line|
		a=line.scanf("%d%d%d")
		id=a[0]
		$all_price[id]=a
	end
end

def buy_product( id, price, count )
	$agent.post("http://tycoon.xiaonei.com/confirmBuyProduct.do", {:product_id => id, :buy_price => price, :count => count, :confirm => "true"})
end

def sell_product( id, price, count, pid )
	$agent.post("http://tycoon.xiaonei.com/confirmSellProduct.do", {:product_id => id, :count => count, :sell_price => price, :pid => pid, :confirm => "true", :is_use_item => "false"})
end

def can_buy( id, price )
	l=$all_price[id][1]
	h=$all_price[id][2]
	v=l+(h-l)/6
	price <= v && price >= 2000
end

def can_sell( id, price )
	l=$all_price[id][1]
	h=$all_price[id][2]
	v=h-(h-l)/2
	price >=v
end

def parse_money( str )
	strs = str.split("亿")
	money = strs[0].gsub(/[^\d]/,"").to_i
	if( strs.size > 1 )
		money = money * 10000 + strs[1].gsub(/[^\d]/, "").to_i
	end
	money/100
end

def parse_my_empire()
	page = $agent.get("http://tycoon.xiaonei.com/myEmpire.do")
	lis = page.parser.search("//ul[@class='dh-prd']/li")
	return if lis[0].search("//form").size == 0
	old_id = 0
	lis.each do |li|
		form = li.search("//form")[0]
		id = form.search("/input[@name='product_id']")[0][:value].to_i
		if( id != old_id )
			pid = form.search("/input[@name='pid']")[0][:value].to_s
			in_price = parse_money( li.search("//span")[0].inner_text.to_s )
			out_price = parse_money( li.search("//span")[1].inner_text.to_s )

			puts "parse_empire, puts id, pid, ...: #{id} #{pid} #{in_price} #{out_price}"
			if( out_price > in_price && can_sell(id, out_price) )
				out_page = sell_product( id, out_price, 5, pid )
				puts out_page.parser.search("").inner_text.gsub(/\s/,"")
			end
		end
		old_id = id
	end
end

username = Base64.decode64("emp1bGl1Ymlu")
password = Base64.decode64("mypass_base64")

begin
	$agent = WWW::Mechanize.new
	$agent.user_agent_alias = "Windows IE 6"
	puts "[程序]: <#{Time.now()}> 正在登录校內..."
	page = $agent.get("http://www.xiaonei.com")
	search_form = page.forms.first
	search_form.fields.name("email").value = username
	search_form.fields.name("password").value = password
	$agent.submit(search_form)
=begin
	page = sell_product( 19, 939, 1, "149671849" )
	puts page.parser.search("").inner_text.gsub(/\s/,"")
	exit 0
=end

	get_data( "superboss.data" )
	parse_my_empire()

	for page_num in 1..6 do
		url="http://tycoon.xiaonei.com/AjaxTycoon.do"
		page = $agent.post(url, {:action => "get", :select_type => page_num })
		lis = page.parser.search("//ul[@class='dh-prd']/li")
		lis.each do |form|
			id = form.search("//button")[0]["onclick"].to_s.gsub(/[^\d]/, "").to_i
			money = form.search("//dd[@class='com-prc']/span")[0].inner_text.to_s.gsub(/\s/,"")
			price = parse_money( money )
			name = form.search("//dt").inner_text.strip

			printf("%d\t%d\t%s\t%s\n", id, price, money, name)

			if( price < $all_price[id][2] && can_buy( id, price ) )
				out_page = buy_product( id, price, 5 )
				puts out_page.parser.search("").inner_text.gsub(/\s/,"")
			end
		end
	end

rescue Exception => e
	puts "#{e}\n"
end
